package com.wxh.controller;

import com.wxh.constant.MessageConstant;
import com.wxh.entity.Result;
import com.wxh.pojo.OrderSetting;
import com.wxh.service.OrderSettingService;
import com.wxh.utils.POIUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;
    @RequestMapping("/upload")
    public Result uplaod(@RequestParam("excelFile")MultipartFile excelFile)  {
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            //将List<String[]>集合转换成List<OrderSetting>集合
            List<OrderSetting> data = new ArrayList<>();
            if(list!=null&&list.size()>0) {
                for (String[] strings : list) {
                    String date = strings[0];
                    String number = strings[1];
                    OrderSetting orderSetting = new OrderSetting(new SimpleDateFormat("yyyy/MM/dd").parse(date), Integer.parseInt(number));
                    data.add(orderSetting);
                }
            }
                orderSettingService.add(data);
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month) {//2019-7
        try {
            List<Map<String, Object>> list = orderSettingService.getOrderSettingByMonth(month);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
        //根据日期修改可预约人数
        @RequestMapping("/editNumberByOrderDate")
        public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting){
            try{
                orderSettingService.editNumberByOrderDate(orderSetting);
                return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false, MessageConstant.ORDERSETTING_FAIL);
            }
        }
        @RequestMapping("/down")
    public void download(HttpServletRequest request,HttpServletResponse response) throws Exception {
        response.setHeader("Content-Disposition","attachment;fileName=template.xlsx");
        response.setContentType("application/octet-stream");
            String realPath = request.getSession().getServletContext().getRealPath("/template/ordersetting_template.xlsx");
            FileInputStream fis=new FileInputStream(realPath);
            ServletOutputStream fos = response.getOutputStream();
            byte[] buffer=new byte[1024];
            int len;
            while((len=fis.read(buffer))!=-1){
                fos.write(buffer,0,len);
            }
            fos.close();
            fis.close();
        }
}
